/*! Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
 * Licensed under the MIT License (LICENSE.txt).
 *
 * Version 2.1.3-pre
 */

(function($){

function prop(n) {
    return n && n.constructor === Number ? n + 'px' : n;
}

$.fn.bgiframe = (function() {
    if (!($.browser.msie && $.browser.version < 7))
        return function() {return this};

    return function(x) {
        x = $.extend({
            top:    'auto', // auto = .currentStyle.borderTopWidth
            left:   'auto', // auto = .currentStyle.borderLeftWidth
            width:  'auto', // auto = offsetWidth
            height: 'auto', // auto = offsetHeight
            opacity: true,
            src:    'javascript:false;'
        }, x);
        
        var et = "expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+'px')",
            el = "expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+'px')",
            ew = "expression(this.parentNode.offsetWidth+'px')",
            eh = "expression(this.parentNode.offsetHeight+'px')",
            f  = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="' + x.src + '"'
                + 'style="display:block;position:absolute;z-index:-1;'
                + 'top:' + (x.top=='auto' ? et : prop(x.top)) + ';'
                + 'left:' + (x.left=='auto' ? el : prop(x.left)) + ';'
                + 'width:' + (x.width=='auto' ? ew : prop(x.width)) + ';'
                + 'height:' + (x.height=='auto' ? eh : prop(x.height)) + ';'
                + (x.opacity ? "filter:Alpha(Opacity='0');" : '') + '"/>';
        return this.each(function() {
            if ($(this).children('iframe.bgiframe').length === 0)
                this.insertBefore(document.createElement(f), this.firstChild);
        });
    };
})();

})(jQuery);
